<template>
  <van-icon
    :color="value === 1 ? 'red' : ''"
    :name="value === 1?'good-job':'good-job-o'"
    @click="onLike"
  />
</template>

<script>
import {addLike, deleteLike} from '@/api/article'
export default {
  data () {
    return {

    }
  },
  props: {
    value: {
      type: Number,
      required: true
    },
    articleId: {
      type: [Number, Object, String],
      required: true
    }
  },
  methods: {
    async onLike () {
      try {
        if (this.value === 1) {
          await deleteLike(this.articleId)
         
          this.$emit('input', -1)
          this.$toast.success('取消点赞')
        } else {
          await addLike(this.articleId)
          this.$emit('input', 1)
          this.$toast.success('点赞成功')
        }
      } catch (err) {
        this.$toast.fail('操作失败')
      }
    }
  }
}
</script>

<style scoped lang='less'>

</style>
